package com.cade.agriculturemall.view.personalCenter.discount.storeDiscountFragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseAdapter;
import com.cade.agriculturemall.base.BaseViewHolder;
import com.cade.agriculturemall.model.Ticket;
import com.cade.agriculturemall.utils.ConverUtil;
import com.cade.agriculturemall.view.shoppingCar.ShopCarsExpandableListAdapter;
import com.cade.agriculturemall.widget.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hshunxin on 2018/2/24.
 */

public class StoreDiscountAdapter extends BaseAdapter implements View.OnClickListener {
    private List<Ticket> storeList;
    private MyClickListener myClickListener;


    public StoreDiscountAdapter(MyClickListener myClickListener, List<Ticket> storeList) {
        this.storeList = storeList;
        this.myClickListener = myClickListener;
    }

    private Ticket ticket = new Ticket();

    @Override
    public void onBindViewHolderWrapper(BaseViewHolder holder, int position) {
        TextView tvTitle = holder.findViewById(R.id.store_item_title_tv);
        TextView tvContent = holder.findViewById(R.id.store_item_content_tv);
        TextView tvTime = holder.findViewById(R.id.store_item_time_tv);
        TextView tvGain = holder.findViewById(R.id.store_item_gain_tv);
        if (storeList.size() != 0) {
            Ticket entity = storeList.get(position);
            tvTitle.setText("￥" + entity.getMoney() + "元" + entity.getType());
            tvContent.setText(entity.getContent());
            tvTime.setText("截止日期:" + ConverUtil.dateToStrLong(entity.getStartTime()) + "-" + ConverUtil.dateToStrLong(entity.getEndTime()));
        }
        tvGain.setOnClickListener(this);
        tvGain.setTag(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent, R.layout.store_discount_item);
    }


    @Override
    public int getItemCount() {
        return storeList.size();
    }


    @Override
    public void onClick(View view) {
        ticket = storeList.get((Integer) view.getTag());
        storeList.remove(ticket);
        if (storeList.size() == 0) {
            myClickListener.emptyListener();
        }
        myClickListener.clickListener(ticket.getObjectId());
        notifyDataSetChanged();
    }

    public interface MyClickListener {
        void clickListener(String ticketId);

        void emptyListener();
    }
}
